/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.shop.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.common.core.constant.enums.DeleteFlagEnum;
import com.pig4cloud.pigx.shop.api.entity.GoodsCategory;
import com.pig4cloud.pigx.shop.mapper.GoodsCategoryMapper;
import com.pig4cloud.pigx.shop.service.GoodsCategoryService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 商品种类
 *
 * @author pig code generator
 * @date 2023-09-12 15:11:41
 */
@Service
public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryMapper, GoodsCategory>
		implements GoodsCategoryService {

	/**
	 * 查询商品分类树菜单
	 * @return
	 */
	@Override
	public List<Tree<Long>> listGoodsCategoryTrees() {
		return getDeptTree(this.list(Wrappers.emptyWrapper()), 0L);
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Override
	public Boolean deleteItems(Long id) {
		List<Long> ids = new ArrayList<>();
		// 先把要删除的一级分类id放入到集合中
		ids.add(id);
		// 递归的将一级分类下的id也加入到集合中
		this.getIds(ids, id);
		return this.removeBatchByIds(ids);
	}

	// 递归方法
	private void getIds(List<Long> ids, Long parentId) {
		// 查询二级分类的对象
		LambdaQueryWrapper<GoodsCategory> lqw = new LambdaQueryWrapper<>();
		lqw.eq(GoodsCategory::getParentId, parentId);
		lqw.eq(GoodsCategory::getDelFlag, DeleteFlagEnum.VALID.getType());
		List<GoodsCategory> contents = this.list(lqw);

		// 遍历二级分类的对象，把二级分类的id加入到要删除的集合中
		for (GoodsCategory content : contents) {
			Long id = content.getId();
			ids.add(id);
			// 把二级分类的每一个ID，查询它下面的子节点
			this.getIds(ids, id);
		}
	}

	/**
	 * 构建部门树
	 * @param goodsCategories 商品分类
	 * @param parentId 父级id
	 * @return
	 */
	private List<Tree<Long>> getDeptTree(List<GoodsCategory> goodsCategories, Long parentId) {
		List<TreeNode<Long>> collect = goodsCategories.stream()
				.filter(goodsCategory -> goodsCategory.getId().intValue() != goodsCategory.getParentId())
				.sorted(Comparator.comparingInt(GoodsCategory::getCategoryOrderBy)).map(goodsCategory -> {
					TreeNode<Long> treeNode = new TreeNode();
					treeNode.setId(goodsCategory.getId());
					treeNode.setParentId(goodsCategory.getParentId());
					treeNode.setName(goodsCategory.getCategoryName());
					treeNode.setWeight(goodsCategory.getCategoryOrderBy());
					// 扩展属性
					Map<String, Object> extra = new HashMap<>(4);
					extra.put("createTime", goodsCategory.getCreateTime());
					treeNode.setExtra(extra);
					return treeNode;
				}).collect(Collectors.toList());

		return TreeUtil.build(collect, parentId);
	}

}
